<template>
  <view>
    <view class="address-box" v-if="JSON.stringify(address)==='{}'">
      <button type="primary" size="mini" class="address-btn" @click="chooseAddress">
        请选择收货地址+
      </button>
    </view>
    <!-- 收货人信息 -->
    <view class="address-info" v-else @click="chooseAddress">
      <view class="row1">
        <view class="row1-left">
          <view class="username">收货人：{{address.userName}}</view>
        </view>
        <view class="row1-right">
          <view class="phone"> 联系方式：{{address.telNumber}}</view>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>
      <view class="row2">
        <view class="row2-left">收货地址：</view>
        <view class="row2-right">{{addstr}}</view>
      </view>
    </view>
    <image src="/static/cart_border@2x.png" class="address-border"></image>
  </view>
</template>
<script>
  import {mapMutations,mapState,mapGetters} from 'vuex'
  export default {
    name:"my-address",
    data(){
      return {
        // address:{}
      };
    },
    computed:{
      ...mapState('User',['address']),
      ...mapGetters('User',['addstr'])
    },
    methods:{
      async chooseAddress(){
        const [err,res] = await uni.chooseAddress().catch(err=>err)
        // console.log(1,res);
       if(err === null&&res.errMsg === 'chooseAddress:ok'){
          // console.log(res);
          this.updataAddress(res);
        }
        //用户授权问题，不过现在不会弹窗了？？
        if(err&&(err.errMsg === 'chooseAddress:fail auto deny'||err.errMsg === 'chooseAddress:fail authorize no response')){
          this.reAuto();
        }
      },
      async reAuto(){
        const [err,res] = await uni.showModal({
          content:'无地址权限？是否去设置中打开？',
          confirmText:'确认',
          cancelText:'取消'
        });
        if(err) return;
        if(res.cancel) return uni.$showMsg('您取消了授权！')
        if(res.confirm) return uni.openSetting({
          success(setting) {
            if(!setting.authSetting['scope.address']) {
                uni.$showMsg('您取消了授权！')
              }else{
                uni.$showMsg('授权成功！请选择收货地址')
              } 
          }
        })
      },
      ...mapMutations("User",['updataAddress'])
    }
  }
</script>
<style lang="scss">
  .address-border{
    display: block;
    width: 100%;
    height: 5px;
  }
  .address-box{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 90px;
  }
  .address-info{
    font-size: 12px;
    height: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 5px;
    .row1{
      display: flex;
      justify-content: space-between;
      .row1-left{
        .username{}
      }
      .row1-right{
        display: flex;
        justify-content: space-between;
        .phone{}
      }
    }
    .row2{
      display: flex;
      // justify-content: space-between;
      align-items: center;
      margin-top: 10px;
      .row2-left{
        white-space: nowrap;
      }
      .row2-right{}
    }
  }
</style>
